package net.hn.hnms.biz.plan.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.hn.hnms.biz.plan.domain.DispatchKeepWatchPerson;
import net.hn.hnms.biz.plan.request.DispatchKeepWatchDutyReq;
import net.hn.hnms.biz.plan.request.DispatchKeepWatchLeadReq;
import net.hn.hnms.biz.plan.request.DispatchKeepWatchReq;
import net.hn.hnms.biz.plan.request.UpdateDutyLDReq;
import net.hn.hnms.biz.plan.service.IDispatchKeepWatchService;
import net.hn.hnms.biz.plan.service.impl.ExecutionFootageOutputServiceImpl;
import net.hn.hnms.biz.plan.vo.DispatchKeepWatchVO;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.web.controller.BaseController;
import net.hn.hnms.sys.common.log.annotation.Log;
import net.hn.hnms.sys.common.log.enums.BusinessType;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 带值班
 *
 * @author zhaoyang
 * @date 2022-08-01
 */
@Slf4j
@Api(value = "带值班模块", tags = {"带值班"})
@RestController
@RequestMapping("/dispatch")
public class DispatchKeepWatchController extends BaseController {


    @Autowired
    private IDispatchKeepWatchService dispatchKeepWatchService;


    @Autowired
    private ExecutionFootageOutputServiceImpl executionFootageOutputService;

    /**
     * 新增值班
     */
    @ApiOperation("新增值班")
//    @SaCheckPermission("dispatch:keepwatch:add")
    @PostMapping("/addDuty")
    @Log(title = "新增值班", businessType = BusinessType.INSERT)
    @AutoSetMineCode
    public R<Void> addDuty(@Validated @RequestBody DispatchKeepWatchDutyReq dutyReq) {
        return toAjax(dispatchKeepWatchService.insertDuty(dutyReq));
    }


    /**
     * 新增带班
     */
    @ApiOperation("新增带班")
//    @SaCheckPermission("dispatch:keepwatch:add")
    @PostMapping("/addLead")
    @Log(title = "新增带班", businessType = BusinessType.UPDATE)
    @AutoSetMineCode
    public R<Void> addLead(@Validated @RequestBody DispatchKeepWatchLeadReq takeReq) {
        dispatchKeepWatchService.insertLead(takeReq);
        return R.ok();
    }


    @ApiOperation("新增带班-灵东")
//    @SaCheckPermission("dispatch:keepwatch:add")
    @PostMapping("/addLeadLD")
    @Log(title = "新增带班", businessType = BusinessType.UPDATE)
    @AutoSetMineCode
    public R<Void> addLeadLD(@Validated @RequestBody DispatchKeepWatchLeadReq takeReq) {
        dispatchKeepWatchService.insertLeadLD(takeReq);
        return R.ok();
    }

    /**
     * 删除带值班
     */
    @ApiOperation("删除带值班")
//    @SaCheckPermission("dispatch:keepwatch:delete")
    @ApiImplicitParam(name = "idList", value = "要删除的id值,用逗号间隔", required = true, allowMultiple = true, dataType = "String", paramType = "query")
    @DeleteMapping("delete")
    @Log(title = "删除带值班", businessType = BusinessType.DELETE)
    public R<Void> delete(@RequestParam("idList") List<String> idList) {
        dispatchKeepWatchService.deleteBatch(idList);
        return R.ok();
    }

    /**
     * 修改带值班
     */
    @ApiOperation("修改带值班")
//    @SaCheckPermission("dispatch:keepwatch:update")
    @PutMapping("/update")
    @Log(title = "修改带值班", businessType = BusinessType.UPDATE)
    public R<Void> update(@Validated @RequestBody DispatchKeepWatchReq dispatchKeepWatch) {
        dispatchKeepWatchService.updateById(dispatchKeepWatch);
        return R.ok();
    }





    /**
     * 启用/禁用HNMT-537
     */
    @ApiOperation("启用/禁用")
//    @SaCheckPermission("dispatch:keepwatch:status:change")
    @PutMapping("/status/change")
    @Log(title = "带值班启用/禁用", businessType = BusinessType.GRANT)
    public R<Void> changeStatus(@RequestBody DispatchKeepWatchReq dispatchKeepWatch) {
        dispatchKeepWatchService.changeStatus(dispatchKeepWatch);
        return R.ok();
    }

    /**
     * 获取带值班列表
     */
    @ApiOperation("获取带值班分页列表")
//    @SaCheckPermission("dispatch:keepwatch:page")
    @GetMapping("/page")
    public R<ResponsePage<DispatchKeepWatchVO>> page(DispatchKeepWatchReq dispatchKeepWatch, PageQuery pageQuery) {
        return R.ok(dispatchKeepWatchService.selectPageList(dispatchKeepWatch, pageQuery));
    }

    /**
     * 获取带值班列表
     */
    @ApiOperation("获取带值班列表")
//    @SaCheckPermission("dispatch:keepwatch:list")
    @GetMapping("/list")
    public R<List<DispatchKeepWatchVO>> list(DispatchKeepWatchReq dispatchKeepWatch) {
        return R.ok(dispatchKeepWatchService.selectList(dispatchKeepWatch));
    }

    /**
     * 获取带值班详情
     */
    @ApiOperation("获取带值班详情")
//    @SaCheckPermission("dispatch:keepwatch:detail")
    @GetMapping("/{id}")
    public R<DispatchKeepWatchVO> detail(@PathVariable("id") String id) {
        return R.ok(dispatchKeepWatchService.selectDetailById(id));
    }

    /**
     * 获取当天值班人员
     */
    @ApiOperation("获取当天值班人员")
//    @SaCheckPermission("dispatch:keepwatch:getPersonByToday")
    @GetMapping("/currentDuty")
    public R<String> currentDuty() {
        List<DispatchKeepWatchPerson> persons = dispatchKeepWatchService.getPersonByToday();
        return R.ok("", persons.stream().map(DispatchKeepWatchPerson::getKeepWatchPersonName).collect(Collectors.joining(",")));
    }

//    @ApiOperation("自动排班")
////    @SaCheckPermission("dispatch:keepwatch:getPersonByToday")
//    @GetMapping("/automatic")
//    public R<List<DispatchKeepWatchVO>> automatic(@DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,@DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate, Integer number) {
//        List<DispatchKeepWatchVO> list = dispatchKeepWatchService.automatic(beginDate, endDate, number);
//        return R.ok(list);
//    }




    @ApiOperation("自动排班结果保存")
//    @SaCheckPermission("dispatch:keepwatch:getPersonByToday")
    @PostMapping("/automatic/add")
    @Log(title = "自动排班结果保存", businessType = BusinessType.INSERT)
    public R<Void> automaticAdd(@RequestBody List<DispatchKeepWatchReq> reqs) {
        dispatchKeepWatchService.automaticAdd(reqs);
        return R.ok();
    }






    // 2022/11/22 新增        ⇣








    @ApiOperation("自动排班")
    @SaCheckPermission("dispatch:keepwatch:automatic")
    @GetMapping("/automatic")
    @Log(title = "自动排班", businessType = BusinessType.INSERT)
    public R<List<DispatchKeepWatchVO>> automatictwo(@DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate) throws ParseException {
        List<DispatchKeepWatchVO> list = dispatchKeepWatchService.automatictwo(beginDate);
        return R.ok(list);
    }

    @ApiOperation("自动排班-核桃裕")
    @SaCheckPermission("dispatch:keepwatch:automatic")
    @GetMapping("/automaticHTY")
    @Log(title = "自动排班", businessType = BusinessType.INSERT)
    @AutoSetMineCode
    public R<List<DispatchKeepWatchVO>> automaticHTY(@DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,String mineCode) throws ParseException {
        List<DispatchKeepWatchVO> list = dispatchKeepWatchService.automaticHTY(beginDate,mineCode);
        return R.ok(list);
    }


    @ApiOperation("自动排班-灵东")
    @SaCheckPermission("dispatch:keepwatch:automatic")
    @GetMapping("/automaticLD")
    @Log(title = "自动排班", businessType = BusinessType.INSERT)
    public R<List<DispatchKeepWatchVO>> automaticLD(@DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate) throws ParseException {
        List<DispatchKeepWatchVO> list = dispatchKeepWatchService.automaticLD(beginDate);
        return R.ok(list);
    }


    @ApiOperation("获取带班列表")
//    @SaCheckPermission("dispatch:keepwatch:leadShiftList")
    @GetMapping("/leadShiftList")
    @AutoSetMineCode
    public R<List<DispatchKeepWatchVO>> leadShiftList(String beginDate,String mineCode) {
        return R.ok(dispatchKeepWatchService.leadShiftList(beginDate,mineCode));
    }

    @ApiOperation("获取值班列表")
    @GetMapping("/dutyShiftList")
    @AutoSetMineCode
    public R<List<DispatchKeepWatchVO>> dutyShiftList( String beginDate,String mineCode) {
        return R.ok(dispatchKeepWatchService.dutyShiftList(beginDate,mineCode));
    }

    @ApiOperation("获取带班统计")
    @GetMapping("/leadShiftStatistics")
    @AutoSetMineCode
    public R<List<DispatchKeepWatchVO>> leadShiftStatistics(String beginDate,String mineCode) {
        return R.ok(dispatchKeepWatchService.leadShiftStatistics(beginDate,mineCode));
    }


    @ApiOperation("获取值班统计")
    @GetMapping("/dutyShiftStatistics")
    @AutoSetMineCode
    public R<List<DispatchKeepWatchVO>> dutyShiftStatistics(String beginDate, String mineCode) {
        return R.ok(dispatchKeepWatchService.dutyShiftStatistics(beginDate, mineCode));
    }
    @ApiOperation("导出带班数据")
    @SaCheckPermission("dispatch:keepwatch:exportLead")
    @PostMapping("/exportLead")
    @AutoSetMineCode
    @Log(title = "导出带班数据", businessType = BusinessType.EXPORT)
    public void exportLead(HttpServletResponse response, String beginDate,String mineCode){
        dispatchKeepWatchService.exportLeadData(response, beginDate,mineCode);
    }

    @ApiOperation("导出生产信息统计数据")
//    @SaCheckPermission("dispatch:keepwatch:exportDuty")
    @PostMapping("/exportProductionStatistics")
    @Log(title = "导出生产信息统计数据", businessType = BusinessType.EXPORT)
    @AutoSetMineCode
    public void exportProductionStatistics(HttpServletResponse response, String beginDate,String mineCode){
        log.info("exportProductionStatistics");
        executionFootageOutputService.exportProductionStatisticsData(response,beginDate,mineCode);
    }




    @ApiOperation("导出值班数据")
    @SaCheckPermission("dispatch:keepwatch:exportDuty")
    @PostMapping("/exportDuty")
    @Log(title = "导出值班数据", businessType = BusinessType.EXPORT)
    public void exportDuty(HttpServletResponse response, String beginDate){
        dispatchKeepWatchService.exportDutyData(response,beginDate);
    }

    @ApiOperation("修改值班")
    @SaCheckPermission("dispatch:keepwatch:updateDuty")
    @PutMapping("/updateDuty")
    @Log(title = "修改值班", businessType = BusinessType.UPDATE)
    public R<Boolean> updateDuty(@Validated @RequestBody DispatchKeepWatchReq dispatchKeepWatch) {
        boolean b = dispatchKeepWatchService.updateDutyById(dispatchKeepWatch);
        return R.ok(b);
    }


    @ApiOperation("修改值班-灵东")
    @SaCheckPermission("dispatch:keepwatch:updateDuty")
    @PutMapping("/updateDutyLD")
    @Log(title = "修改值班灵东", businessType = BusinessType.UPDATE)
    public R<Boolean> updateDutyLD(@Validated @RequestBody UpdateDutyLDReq updateDutyLDReq) {
        boolean b = dispatchKeepWatchService.updateDutyByIdLD( updateDutyLDReq);
        return R.ok(b);
    }
    @ApiOperation("新增值班")
//    @SaCheckPermission("dispatch:keepwatch:add")
    @PostMapping("/addDutyLD")
    @Log(title = "新增值班", businessType = BusinessType.INSERT)
    @AutoSetMineCode
    public R<Void> addDutyLD(@Validated @RequestBody UpdateDutyLDReq dutyReq) {
        return toAjax(dispatchKeepWatchService.insertDutyLD(dutyReq));
    }
    @ApiOperation("修改带班")
    @SaCheckPermission("dispatch:keepwatch:updateLead")
    @PutMapping("/updateLead")
    @Log(title = "修改带班", businessType = BusinessType.UPDATE)
    public R<Boolean> updateLead(@Validated @RequestBody DispatchKeepWatchReq dispatchKeepWatch) {
        boolean b = false;
         b =  dispatchKeepWatchService.updateLeadById(dispatchKeepWatch);
        return R.ok(b);
    }

    @ApiOperation("修改带班-灵东")
    @SaCheckPermission("dispatch:keepwatch:updateLead")
    @PutMapping("/updateLeadLD")
    @Log(title = "修改带班", businessType = BusinessType.UPDATE)
    @AutoSetMineCode
    public R<Boolean> updateLeadLD(@Validated @RequestBody DispatchKeepWatchReq dispatchKeepWatch) {
        boolean b = false;
        b =  dispatchKeepWatchService.updateLeadLDById(dispatchKeepWatch);
        return R.ok(b);
    }

    @ApiOperation("置空带班")
    @SaCheckPermission("dispatch:keepwatch:emptyLead")
    @PutMapping("/emptyLead")
    @Log(title = "置空带班", businessType = BusinessType.CLEAN)
    public R<Void> emptyLead(@Validated @RequestBody DispatchKeepWatchReq dispatchKeepWatch) {
        dispatchKeepWatchService.emptyLead(dispatchKeepWatch);
        return R.ok();
    }

    @ApiOperation("置空值班")
    @SaCheckPermission("dispatch:keepwatch:emptyDuty")
    @PutMapping("/emptyDuty")
    @Log(title = "置空值班", businessType = BusinessType.CLEAN)
    public R<Void> emptyDuty(@Validated @RequestBody DispatchKeepWatchReq dispatchKeepWatch) {
        dispatchKeepWatchService.emptyDuty(dispatchKeepWatch);
        return R.ok();
    }

    /**
     * 自动排班-高头窑
     * @param beginDate 每年一月一日
     * @return
     * @throws ParseException
     */
    @ApiOperation("自动排班-高头窑")
    @SaCheckPermission("dispatch:keepwatch:automaticGty")
    @GetMapping("/automaticGty")
    @Log(title = "自动排班-高头窑", businessType = BusinessType.INSERT)
    @AutoSetMineCode
    public R<List<DispatchKeepWatchVO>> automaticGty(@DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,String mineCode) throws ParseException {
        List<DispatchKeepWatchVO> list = dispatchKeepWatchService.automaticgty(beginDate,mineCode);
        return R.ok(list);
    }

    /**
     * 获取带班列表-高头窑
     * @param beginDate 传年份
     * @param mineCode
     * @return
     */
    @ApiOperation("获取带班列表-高头窑")
    @SaCheckPermission("dispatch:keepwatch:leadShiftListGty")
    @GetMapping("/leadShiftListGty")
    @AutoSetMineCode
    public R<List<DispatchKeepWatchVO>> leadShiftListGty(String beginDate,String mineCode) {
        return R.ok(dispatchKeepWatchService.leadShiftListGty(beginDate,mineCode));
    }

    /**
     *
     * @param response
     * @param beginDate 年份
     */
    @ApiOperation("导出带班数据-高头窑")
    @SaCheckPermission("dispatch:keepwatch:exportLeadGty")
    @PostMapping("/exportLeadGty")
    @Log(title = "导出带班数据-高头窑", businessType = BusinessType.EXPORT)
    public void exportLeadGty(HttpServletResponse response, String beginDate,String mineCode){
        dispatchKeepWatchService.exportLeadDataGty(response, beginDate,mineCode);
    }
    @ApiOperation("修改带班-高头窑")
    @SaCheckPermission("dispatch:keepwatch:updateLeadGty")
    @PutMapping("/updateLeadGty")
    @Log(title = "修改带班-高头窑", businessType = BusinessType.UPDATE)
    @AutoSetMineCode
    public R<Boolean> updateLeadGty(@Validated @RequestBody DispatchKeepWatchReq dispatchKeepWatch) {
        boolean b = false;
        b =  dispatchKeepWatchService.updateLeadGtyById(dispatchKeepWatch);
        return R.ok(b);
    }


    @ApiOperation("根据当前时间获取带值班人员")
    @GetMapping("/getOnDutyPersonnel")
    @AutoSetMineCode
    public R<DispatchKeepWatchVO> getOnDutyPersonnel(String beginDate,String mineCode) {
        return R.ok(dispatchKeepWatchService.getOnDutyPersonnelOfLD(beginDate,mineCode));
    }



//    @ApiOperation("根据当前时间获取带值班人员（灵东灵泉）")
//    @GetMapping("/getOnDutyPersonnelLD")
//    @AutoSetMineCode
//    public R<DispatchKeepWatchVO> getOnDutyPersonnelLD(String beginDate,String mineCode) {
//        return R.ok(dispatchKeepWatchService.getOnDutyPersonnelOfLD(beginDate,mineCode));
//    }


    @ApiOperation("带值班人员导入")
//    @SaCheckPermission("training:certificate:importOther")
    @PostMapping("/importOther")
    @Log(title = "带值班人员导入", businessType = BusinessType.IMPORT)
    @AutoSetMineCode
    public R<Void> importOtherResult(@RequestPart MultipartFile file,String mineCode) {
        dispatchKeepWatchService.importResult(file,mineCode);
        return R.ok();
    }





}
